/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.careersersystem.struts.action;

import com.careersersystem.dao.UserDAO;
import com.careersersystem.entities.Users;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.careersersystem.struts.form.UsersLoginForm;
import java.util.Iterator;
import java.util.List;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

/**
 *
 * @author Administrator
 */
public class LoginAction extends org.apache.struts.action.Action {
    
    /* forward name="success" path="" */
    private final static String LOGINSUCCESS = "loginsuccess";
    private final static String FAILED ="failed";
    private final static String UNCONFIRM = "unconfirm";
    private UserDAO userdao = new UserDAO();
    private final static String STUDENT = "student";
    private final static String STAFF = "staff";
    private final static String EMPLOYER = "employer";
    private final static String ADMIN = "admin";
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        UsersLoginForm usersloginForm = (UsersLoginForm) form;
        String userName=usersloginForm.getUsername();
        String userPassword=usersloginForm.getUserPassword();
        Users user = new Users();
            Iterator<Users> result = userdao.searchByUsernamePassword(userName, userPassword);
            if(!result.hasNext()){
                return mapping.findForward(FAILED);
            }else{
                user = (Users)result.next();
                if(user.getConfirmed()== 0){
                    return mapping.findForward(UNCONFIRM);
                }else{
                    request.getSession().setAttribute("userId",user.getUserId());
                    //request.getSession().setAttribute("userName",user.getUserName());
                    //request.getSession().setAttribute("userPassword",user.getUserPassword());
                    request.getSession().setAttribute("userRole",user.getUserType());
                    request.getSession().setAttribute("confrimed",user.getConfirmed());
                    List list=getUserInfo(user);
                    request.getSession().setAttribute("list", list);
                    return mapping.findForward(checkUserType(user));
                }
            }
    }
    public String checkUserType(Users user){
        if(user.getUserType()==1){
            return STUDENT;
        }else if(user.getUserType()==2){
            return EMPLOYER;
        }else if(user.getUserType()==3){
            return STAFF;
        }else{
           return ADMIN;
        }
    }
    public List getUserInfo(Users user){
        if(user.getUserType()==1){
            List result=(List) user.getStudents();
            return result;
        }else if(user.getUserType()==2){
            List result=(List )user.getEmployers();
            return result;
        }else if(user.getUserType()==3){
           List result=(List )user.getStaffs();
            return result;
        }else{
           List result=(List )user.getAdministrators();
            return result;
        }
    }
}
